import { getToken } from "../../utils";
import { invoke } from '@tauri-apps/api/core';

export interface ChatMessageContentText {
    type: 'text',
    text: string,
}

export interface ChatMessageContentImageUrl {
    type: 'image_url',
    image_url: { url: string }
}

export type ChatMessageContent = Array<ChatMessageContentText | ChatMessageContentImageUrl> | string;

export const peerLogin = async () => {
    let token = getToken();
    console.log('开始进行节点登录:', token);
    // 
    await invoke('plugin:mg-platform|peer_login', { token: token });
    //
    return;
}

export const peerLogout = async () => {
    let token = getToken();
    console.log('开始进行节点登录:', token);
    // 
    await invoke('plugin:mg-platform|peer_logout', { token: token });
    //
    return;
}

export const peerGetPeerId = async (): Promise<string> => {
    // 
    let peer_ids: Array<any> = await invoke('plugin:mg-platform|peer_id', {});
    //
    console.log(peer_ids);
    return peer_ids[0];
}

export const peerAssistantChat = async (
    messages: Array<{ content: ChatMessageContent, role: string, [key: string]: any }>,
    options: { [key: string]: any }
): Promise<Array<{ content: ChatMessageContent, [key: string]: any }>> => {
    // 
    let res: Array<any> = await invoke('plugin:mg-platform|peer_assistant_chat', { messages: messages, options: options });

    console.log(res);
    //
    return res;
}

export const peerAssistantSv2tts = async (promptAudioPath: string, promptText: string, text: string,
    localPath: string,
    options: { [key: string]: any }): Promise<any> => {

    // options:{model,extensions}
    let res = await invoke('plugin:mg-platform|peer_assistant_sv2tts', {
        messages: {
            prompt_audio_path: promptAudioPath,
            prompt_language: 'zh',
            prompt_text: promptText,
            text: text,
            text_language: 'zh',
        },
        localPath: localPath,
        options: options
    });

    console.log(res);
    //
    return res;
}


export const peerAssistantShell = async (messages: {
    env: { [key: string]: string },
    current?: string,
    before?: Array<{ cmd: string, args?: string[] }>,
    script: Array<string[]>,
    after?: Array<{ cmd: string, args?: string[] }>,
}, options: { model?: string, extensions?: { [key: string]: string } }): Promise<Array<{ content: string, [key: string]: any }>> => {

    // 
    let res: Array<any> = await invoke('plugin:mg-platform|peer_assistant_shell', { messages: messages, options: options });

    console.log(res);
    //
    return res;
}

export const peerAssistantBatchScript = async (messages: {
    env: { [key: string]: string },
    current?: string,
    before?: Array<{ cmd: string, args?: string[] }>,
    script: string,
    after?: Array<{ cmd: string, args?: string[] }>,
}, options: { model?: string, extensions?: { [key: string]: string } }): Promise<any> => {

    // 
    let res: any = await invoke('plugin:mg-platform|peer_assistant_batch_script', { messages: messages, options: options });

    console.log(res);
    //
    return res;
}

export const channelPubsubPublish = async (topic: string, subscribers: Array<string>,): Promise<void> => {
    // 
    await invoke('plugin:mg-platform|channel_pubsub_publish', { topic: topic, subscribers: subscribers });
}

export const channelPubsubPublishMessage = async (topic: String, message: String,): Promise<void> => {

    // 
    await invoke('plugin:mg-platform|channel_pubsub_publish_message', { topic: topic, message: message });
}

export const channelPubsubSubscribe = async (peer_id: string, topic: string): Promise<void> => {
    // 
    let res: Array<any> = await invoke('plugin:mg-platform|channel_pubsub_subscribe', {
        topic: {
            peer_id: peer_id,
            topic: topic,
            subscribers: [],
        }
    });
}

export const channelRequestResponse = async (peer_id: string, topic: string): Promise<any> => {

    // 
    let res: Array<any> = await invoke('plugin:mg-platform|channel_pubsub_subscribe', {
        topic: {
            peer_id: peer_id,
            topic: topic,
            subscribers: [],
        }
    });

    console.log(res);
    return res;
}

export const channelUicmdResponse = async (request_id: number, message: string): Promise<void> => {
    // 
    await invoke('plugin:mg-platform|channel_uicmd_response', {
        requestId: request_id,
        message: message
    });
}

export const channelUicmdRemoteRequest = async (peerId: string, message: string): Promise<any> => {
    // 
    let res: string = await invoke('plugin:mg-platform|channel_uicmd_remote_request', {
        peerId: peerId,
        message: message
    });
    let response = JSON.parse(res);
    console.log(response);
    return response;
}